const log = require('../../util/log')
const {
  Network,
  Page,
  DOM,
  Runtime,
  Emulation
} = require('../chromeMng.20180824').client;

async function checkLoginJd() {
  log('checking login jd')
  try {
    Page.navigate({
      url: 'https://media.jd.com/'
    })
    return step1()
  } catch (err) {
    log(err)
  }
}

async function step1() {
  log('checking login jd step 1')
  try {
    var {result} = await Runtime.evaluate({expression: 'location.href'})
    log(result.value)
    if (result.value == 'https://media.jd.com/') {
      return step2()
    }
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        step1().then(result => {
          resolve(result)
        }, result => {
          reject(result)
        })
      }, 1000)
    })
  } catch (err) {
    log(err)
  }
}

async function step2() {
  log('checking login jd step 2')
  try {
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("#userPin")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 1) {
      var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("#userPin")[0].innerText'})
      if (result.value != '') {
        return new Promise((resolve, reject) => {
          resolve(true)
        })
      }
    }
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          step2().then(result => {
            resolve(result)
          }, result => {
            reject(result)
          })
        }, 1000)
      })
    }
    if (cnt > 1) {
      return new Promise((resolve, reject) => {
        reject('iframe多于1个')
      })
    }
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#o-authcode")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 1) {
      var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#o-authcode")[0].className'})
      if (result.value.indexOf('hide') == -1) {
        return new Promise((resolve, reject) => {
          log('要输验证码')
          resolve(false)
        })
      }
    }
    var {result} = await Runtime.evaluate({expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#loginname")'})
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          step2().then(result => {
            resolve(result)
          }, result => {
            reject(result)
          })
        }, 1000)
      })
    }
    return new Promise((resolve, reject) => {
      resolve(false)
    })
  } catch (err) {
    log(err)
  }
}

module.exports = checkLoginJd
